module(..., package.seeall)
-- 模块功能：显示逻辑
-- @author zhangfenghe
-- @module access_control_system.displayLogic
-- @license MIT
-- @copyright openLuat
-- @release 2024.02.27
require "pins"
require "mqtt"
require "misc"
require "nvm"
require "net"
require "config"
require "mqttHostOutMsg"
require "uartTask"
require "utilsACS"
require "doorLocStatFeedback"
-- 初始化配置文件
nvm.init("config.lua", true)
InterruptNomalMsg, TimeId = false, nil
-- 获取配置文件信息
local slaveNo, hostNo = nvm.get("ClientNo"), nvm.get("HostNo")
local topic, sendMsg, hour, minute, second, countNomalDisplay,
      countEmergencyDis, lightSwith = "", "", "", "", "", 0, 0, ""
-- 紧急报警显示
function EmergencyAlarmDisplay()
    countEmergencyDis = countEmergencyDis + 1
    if countEmergencyDis > 0 and countEmergencyDis <= 8 then
        if math.fmod(countEmergencyDis, 2) == 0 then
            lightSwith = "0x0c"
        elseif math.fmod(countEmergencyDis, 2) == 1 then
            lightSwith = "0x0b"
        end
    elseif countEmergencyDis > 8 and countEmergencyDis <= 16 then
        lightSwith = "0x0b"
    end
    local data = (string.format("%02x", "0x55") ..
                     string.format("%02x", lightSwith) ..
                     string.format("%02x", lightSwith) ..
                     string.format("%02x", lightSwith) ..
                     string.format("%02x", lightSwith) ..
                     string.format("%02x", lightSwith) ..
                     string.format("%02x", "0x80") ..
                     string.format("%02x", "0x00") ..
                     string.format("%02x", "0x00") ..
                     string.format("%02x", "0xff")):fromHex()
    log.info("紧急报警显示开始：", data:toHex())
    uartTask.Write(data)
    if countEmergencyDis >= 16 then countEmergencyDis = 0 end
end
-- 全亮
function FullBrightness(...)
    local data =
        (string.format("%02x", "0x55") .. string.format("%02x", "0x0c") ..
            string.format("%02x", "0x0c") .. string.format("%02x", "0x0c") ..
            string.format("%02x", "0x0c") .. string.format("%02x", "0x0c") ..
            string.format("%02x", "0x80") .. string.format("%02x", "0x00") ..
            string.format("%02x", "0x00") .. string.format("%02x", "0xff")):fromHex()
    uartTask.Write(data)
end
-- 全灭
function TotalExtinction(...)
    local data =
        (string.format("%02x", "0x55") .. string.format("%02x", "0x0b") ..
            string.format("%02x", "0x0b") .. string.format("%02x", "0x0b") ..
            string.format("%02x", "0x0b") .. string.format("%02x", "0x0b") ..
            string.format("%02x", "0x00") .. string.format("%02x", "0x00") ..
            string.format("%02x", "0x00") .. string.format("%02x", "0xff")):fromHex()
    uartTask.Write(data)
end
-- 地震预警显示
function QuakeAlarmDisplay(red, gree, blue, countDown, intensity, hz)
    local intensity1, intensity2, countDown1, countDown2, redStr, greeStr,
          blueStr, hzStr
    intensity1 = "0x0" .. string.sub(intensity, 2, 2)
    intensity2 = "0x11"
    countDown1 = "0x0" .. string.sub(countDown, 2, 2)
    countDown2 = "0x0" .. string.sub(countDown, 3, 3)
    redStr = "0x" .. string.sub(red, 2, 3)
    greeStr = "0x" .. string.sub(gree, 2, 3)
    blueStr = "0x" .. string.sub(blue, 2, 3)
    hzStr = "0x" .. hz
    local data = (string.format("%02x", "0x55") ..
                     string.format("%02x", intensity1) ..
                     string.format("%02x", intensity2) ..
                     string.format("%02x", "0x11") ..
                     string.format("%02x", countDown1) ..
                     string.format("%02x", countDown2) ..
                     string.format("%02x", redStr) ..
                     string.format("%02x", greeStr) ..
                     string.format("%02x", blueStr) ..
                     string.format("%02x", hzStr)):fromHex()
    uartTask.Write(data)
end

-- 正常显示
local function nomalDisplay()
    local colon, hour1, hour2, minute1, minute2
    countNomalDisplay = countNomalDisplay + 1
    hour = tostring(os.date("*t").hour)
    minute = tostring(os.date("*t").min)
    -- 冒号显示
    if math.fmod(countNomalDisplay, 2) == 0 then
        colon = "0x0A"
    elseif math.fmod(countNomalDisplay, 2) == 1 then
        colon = "0x0B"
    end
    -- 时赋值
    if string.len(hour) == 1 then
        hour1 = "0x00"
        hour2 = "0x0" .. hour
    elseif string.len(hour) == 2 then
        hour1 = "0x0" .. string.sub(hour, 1, 1)
        hour2 = "0x0" .. string.sub(hour, 2, 2)
    end
    -- 分赋值
    if string.len(minute) == 1 then
        minute1 = "0x00"
        minute2 = "0x0" .. minute
    elseif string.len(minute) == 2 then
        minute1 = "0x0" .. string.sub(minute, 1, 1)
        minute2 = "0x0" .. string.sub(minute, 2, 2)
    end
    local netMode, rssi, data = net.getNetMode(), net.getRssi()
    if netMode ~= 0 and rssi >= 17 then
        data = (string.format("%02x", "0x55") .. string.format("%02x", hour1) ..
                   string.format("%02x", hour2) .. string.format("%02x", colon) ..
                   string.format("%02x", minute1) ..
                   string.format("%02x", minute2) ..
                   string.format("%02x", "0x80") ..
                   string.format("%02x", "0x80") ..
                   string.format("%02x", "0x80") ..
                   string.format("%02x", "0xFF")):fromHex()
        log.info("正常显示", data:toHex())
    else
        data = (string.format("%02x", "0x55") .. string.format("%02x", hour1) ..
                   string.format("%02x", hour2) .. string.format("%02x", colon) ..
                   string.format("%02x", minute1) ..
                   string.format("%02x", minute2) ..
                   string.format("%02x", "0x80") ..
                   string.format("%02x", "0x00") ..
                   string.format("%02x", "0x00") ..
                   string.format("%02x", "0xFF")):fromHex()
    end
    uartTask.Write(data)
    if countNomalDisplay >= 2 then countNomalDisplay = 0 end
end
-- 开门取消
function OpenDisplayCancel()
    local data =
        (string.format("%02x", "0x55") .. string.format("%02x", "0x00") ..
            string.format("%02x", "0x00") .. string.format("%02x", "0x00") ..
            string.format("%02x", "0x00") .. string.format("%02x", "0x00") ..
            string.format("%02x", "0x00") .. string.format("%02x", "0x00") ..
            string.format("%02x", "0x00") .. string.format("%02x", "0xff") ..
            ".0x00"):fromHex()
    uartTask.Write(data)
end

-- 开门显示
function OpenDisplay()
    local data =
        (string.format("%02x", "0x55") .. string.format("%02x", "0x0b") ..
            string.format("%02x", "0x00") .. string.format("%02x", "0x00") ..
            string.format("%02x", "0x00") .. string.format("%02x", "0x00") ..
            string.format("%02x", "0x00") .. string.format("%02x", "0x80") ..
            string.format("%02x", "0x00") .. string.format("%02x", "0xff") ..
            ".0x01"):fromHex()
    log.info("开门显示", data:toHex())
    uartTask.Write(data)
end

-- 正常显示逻辑
function NomalTask()
    if not utilsACS.QuakeInterruptFlag and not utilsACS.EmerInterruptFlag and
        not TimeId then
        if not doorLocStatFeedback.OpenDoorDisplay then
            if doorLocStatFeedback.OpenDoorSendMsgTime ~= nil then
                doorLocStatFeedback.OpenDoorSendMsgTime = sys.timerStop(
                                                              doorLocStatFeedback.OpenDoorSendMsgTime)
            end
            displayLogic.OpenDisplayCancel()
            TimeId = sys.timerLoopStart(nomalDisplay, 1000)
        else
            log.info("开门显示测试1！！！")
            doorLocStatFeedback.OpenDoorSendMsgTime = sys.timerLoopStart(
                                                          displayLogic.OpenDisplay,
                                                          1000)

        end
    end
    log.info("Test2", utilsACS.QuakeInterruptFlag, utilsACS.EmerInterruptFlag)
    if utilsACS.QuakeInterruptFlag or utilsACS.EmerInterruptFlag or
        doorLocStatFeedback.OpenDoorDisplay then
        sys.timerStop(TimeId)
        TimeId = nil
    end
end
